/**
 * @ClassName:     MainActivity
 * @Description:   MainActivity
 *
 * @author          JetKey
 * @version         V1.0
 * @Date            2018/08/10
 */

package com.imi.sdk.jnitest

import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import org.jetbrains.anko.*
import kotlin.concurrent.thread

class MainActivity : Activity(){

    init {
        System.loadLibrary("testsuit_jni")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val v = UI {
            verticalLayout {
                    val n = getTestCount()

                    textView("Test Case Count:$n") {
                        txtOut = this
                    }.lparams(matchParent,dip(200))

                    scrollView {
                        verticalLayout {

                            getTestList().forEachIndexed { index, s ->
                                //
                                button(s) {
                                    setOnClickListener {
                                        txtOut?.text = ""
                                        thread(true) {
                                            runTest(index)
                                        }
                                    }
                                }
                            }
                        }

                }
            }
        }.view
        setContentView(v)

    }

    var txtOut:TextView? = null

    //
    fun print(text:String):Int{
        runOnUiThread {
            txtOut?.append("${text}\n")
        }
        return 1
    }

    fun getTestList() = Array<String>(getTestCount()) { getTestName(it) }


    external fun getUrl():String
    external fun getTestCount():Int
    external fun getTestName(i:Int):String
    external fun runTest(i:Int):Int

}